package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);
        //获得菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item) -> {
            //将保存后的setmealdto的套餐id赋值给setmealdishe
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);

        int count = this.count(queryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 启售,停售套餐
     *
     * @param status
     * @param ids
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, List<Long> ids) {
        //1.首先判断本次操作是停售还是启售,如果是停售,则直接可以停售该所有套餐
        if (status == 0) {
            LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.in(Setmeal::getId, ids);
            updateWrapper.set(Setmeal::getStatus, status);
            this.update(updateWrapper);
            return;
        }
        //2.如果是启售,则需要判断该套餐中的菜品是否存在停售的,如果套餐中的至少有一个菜品是停售状态,则该套餐不能启售;
        int count = 0;
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, id);
            List<SetmealDish> list = setmealDishService.list(queryWrapper);
            boolean flag = true;
            for (SetmealDish setmealDish : list) {
                //查询每一个套餐菜品的状态;
                Long dishId = setmealDish.getDishId();
                Dish dish = dishService.getById(dishId);
                if (dish.getStatus() == 0) {
                    //如果有菜品的状态是停售,则将标记改为false;
                    flag = false;
                    //退出本次循环
                    break;
                }
            }
            //判断标记,如果是true,则表示该套餐的所有菜品都是启售状态,该套餐也可以启售,否则不修改套餐状态;
            if (flag) {
                Setmeal setmeal = this.getById(id);
                setmeal.setStatus(status);
                this.updateById(setmeal);
                count++; //修改了一个套餐状态标记加1次;
            }
        }
        //3.循环结束,判断一下count
        if (count == 0) {
            throw new CustomException("所有套餐中,都有停售菜品,无法启售");
        } else if (count < ids.size()) {
            throw new CustomException("部分套餐中存在停售菜品,无法启售,请检查");
        }
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealDto(Long id) {
        //1.通过id查询Setmeal表,找到菜单信息
        Setmeal setmeal = this.getById(id);
        //2.通过菜品id去菜品口味表中获取该菜品的口味集合
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //3.新建一个setmealDto对象,拷贝Setmeal对象的信息
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //4.设置setmealDto数据中的口味值
        setmealDto.setSetmealDishes(list);
        //5.返回setmealDto对象
        return setmealDto;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        //1.先更新套餐表;
        this.updateById(setmealDto);
        //2.更新套餐菜品表;
        //2.1首先根据套餐的id删除所有的套餐菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //3.1取出前端传递过来的新的套餐菜品信息,存到数据库中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //3.2取出的套餐信息不完整,没有套餐id,需要设置
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //3.3批量保存已经设置好的套餐信息
        setmealDishService.saveBatch(setmealDishes);
    }
}
